package sceneGraph;

import java.util.ArrayList;
import java.util.Random;

import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.glu.GLU;

import squelette.JavaRenderer;
import squelette.Mass;
import squelette.Poil;

import Lib3d.Vector3d;

public class SceneHairySphereNode extends SceneNode {

	private static final int NB_HAIRS = 100;
	private static final float SPHERE_DIAMETER = 10.0f;
	
	private static final float POIL_HAIR_FIRST_RADIUS = SPHERE_DIAMETER;	// Distance entre deux masses du poil, du centre vers rayon de la sphere.
	private static final float POIL_HAIR_SECOND_RADIUS = 10.0f + POIL_HAIR_FIRST_RADIUS; 	// Distance entre entre centre de la sphere et troisieme masse.
	private static final float POIL_HAIR_THIRD_RADIUS = 10.0f + POIL_HAIR_SECOND_RADIUS;	// Distance entre entre centre de la sphere et quatrieme masse.
	private static final float RADIUS_RANDOMNESS = 9.0f;	// Hasard ajout� aux rayons pour un rendu moins uniforme.
	
	private static final GLU glu = new GLU();

	private ArrayList<Poil> poils = new ArrayList<Poil>(); 
	
	private Vector3d oldPosition = new Vector3d(0, 0, 0); // Utile pour connaitre la vitesse de la sphere.
	private Vector3d currentPosition = new Vector3d(0, 0, 0); // Utile pour connaitre la vitesse de la sphere.
	public void setCurrentPosition(Vector3d currentPosition) { this.currentPosition = currentPosition; }

	public SceneHairySphereNode() {
		Random random = new Random();
		
		// Génération des poilz.
		for (int i = 0 ; i < NB_HAIRS ; i++) {
			float theta = random.nextFloat() * 180;	// 0 <= theta <= 180
			float phy = random.nextFloat() * 360;	// 0 <= phy <= 360
			float randDist = random.nextFloat() * RADIUS_RANDOMNESS - RADIUS_RANDOMNESS / 2;
			Vector3d vm1 = JavaRenderer.getVectorFromSphericalPolarCoordinates(theta, phy, POIL_HAIR_FIRST_RADIUS);
			Vector3d vm2 = JavaRenderer.getVectorFromSphericalPolarCoordinates(theta, phy, POIL_HAIR_SECOND_RADIUS + randDist);
			randDist = random.nextFloat() * RADIUS_RANDOMNESS - RADIUS_RANDOMNESS / 2;
			Vector3d vm3 = JavaRenderer.getVectorFromSphericalPolarCoordinates(theta, phy, POIL_HAIR_THIRD_RADIUS + randDist);
			
			Poil p1 = new Poil(vm1, vm2, vm3);
			poils.add(p1);
		}
	}
	
	@Override
	public void draw(GLAutoDrawable gLDrawable) {

		gl.glEnable(GL.GL_COLOR_MATERIAL) ;
		// Affiche la sphère. 
		gl.glColor3f(1.0f,0.5f,0.8f);
		glu.gluSphere(glu.gluNewQuadric(), SPHERE_DIAMETER, 20, 20);
		
		// Calcule les forces sur chaque poils.
		Poil.setSphereSpeed(Vector3d.sub(currentPosition, oldPosition));
		
		for (Poil poil : poils) {
			poil.calculateSumForces();
		}
		
		// Affiche les poils.
		if (gl != null) {

			gl.glBegin(GL.GL_LINES);
			for (Poil poil : poils) {
				gl.glColor3f(poil.getR(), poil.getG(), poil.getB());
				Mass m1 = poil.getMasse1ToDraw();
				Mass m2 = poil.getMasse2ToDraw();
				Mass m3 = poil.getMasse3ToDraw();
				drawLineBetweenMasses(m1, m2);
				drawLineBetweenMasses(m2, m3);
			}
			gl.glDisable(GL.GL_COLOR_MATERIAL) ;
			gl.glEnd();
		}
		
		oldPosition = currentPosition;
	}

	/*
	 * Relie deux masses par une ligne. 
	 */
	private void drawLineBetweenMasses(Mass m1, Mass m2) {
		gl.glVertex3f(m1.getPosition().get(0), m1.getPosition().get(1), m1.getPosition().get(2));
		gl.glVertex3f(m2.getPosition().get(0), m2.getPosition().get(1), m2.getPosition().get(2));
	}
	
	

}
